package com.zhangxu.microservice.system.web.controller.ae;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangxu.microservice.common.context.EmployeeContextHolder;
import com.zhangxu.microservice.common.dto.EmployeeContextDTO;
import com.zhangxu.microservice.common.result.CommonResult;
import com.zhangxu.microservice.common.utils.AesEncryptUtils;
import com.zhangxu.microservice.common.utils.AssertUtils;
import com.zhangxu.microservice.system.biz.dto.CreateEmployeeDTO;
import com.zhangxu.microservice.system.biz.dto.EmployeeDTO;
import com.zhangxu.microservice.system.biz.service.EmployeeBizService;
import com.zhangxu.microservice.system.dao.condition.EmployeeCondition;
import com.zhangxu.microservice.system.web.convert.EmployeeConverter;
import com.zhangxu.microservice.system.web.request.CreateEmployeeReq;
import com.zhangxu.microservice.system.web.request.PageReq;
import com.zhangxu.microservice.system.web.response.EmployeeResp;
import com.zhangxu.microservice.system.web.response.PageResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("ae")
@Tag(name = "用户管理")
@RequiredArgsConstructor
public class EmployeeController {

    private final EmployeeBizService employeeBizService;
    private final EmployeeConverter employeeConverter;

    @Operation(summary = "获取员工信息", description = "获取员工信息")
    @GetMapping("employees/{employeeId}")
    public CommonResult<EmployeeResp> get(@Parameter(description = "用户ID") @PathVariable("employeeId") Long employeeId) {
        EmployeeDTO employeeDTO = employeeBizService.getById(employeeId);
        AssertUtils.notNull(employeeDTO, "员工不存在");
        return CommonResult.success(employeeConverter.dto2EmployeeResp(employeeDTO));
    }

    @Operation(summary = "获取本人信息", description = "获取本人信息")
    @GetMapping("/employee")
    public CommonResult<EmployeeResp> self() {
        EmployeeContextDTO employeeContextDTO = EmployeeContextHolder.get();
        EmployeeResp employeeResp = new EmployeeResp();
        employeeResp.setName(employeeContextDTO.getName());
        employeeResp.setId(employeeContextDTO.getId());
        employeeResp.setAccount(employeeContextDTO.getAccount());
        employeeResp.setEmail(employeeContextDTO.getEmail());
        return CommonResult.success(employeeResp);
    }

    @Operation(summary = "创建员工", description = "创建员工")
    @PostMapping("employees")
    public CommonResult<Long> create(@Validated @RequestBody CreateEmployeeReq request) {
        CreateEmployeeDTO createEmployeeDTO = employeeConverter.toCreateEmployeeDTO(request);
        return CommonResult.success(employeeBizService.createEmployee(createEmployeeDTO));
    }

    @Operation(summary = "编辑员工", description = "编辑员工")
    @PostMapping("employees/{employeeId}")
    public CommonResult<Long> update(@Parameter(description = "用户ID") @PathVariable("employeeId") Long employeeId,
                                     @Validated @RequestBody CreateEmployeeReq request) {
        CreateEmployeeDTO createEmployeeDTO = employeeConverter.toCreateEmployeeDTO(request);
        return CommonResult.success(employeeBizService.createEmployee(createEmployeeDTO));
    }

    @Operation(summary = "启用员工", description = "启用员工")
    @PostMapping("employees/{employeeId}/enable")
    public CommonResult<Long> enable(@Parameter(description = "用户ID") @PathVariable("employeeId") Long employeeId) {
        employeeBizService.enable(employeeId);
        return CommonResult.success();
    }

    @Operation(summary = "禁用员工", description = "禁用员工")
    @PostMapping("employees/{employeeId}/disable")
    public CommonResult<Long> disable(@Parameter(description = "用户ID") @PathVariable("employeeId") Long employeeId) {
        employeeBizService.disable(employeeId);
        return CommonResult.success();
    }

    @Operation(summary = "删除员工", description = "删除员工")
    @DeleteMapping("employees/{employeeId}")
    public CommonResult<Long> delete(@Parameter(description = "用户ID") @PathVariable("employeeId") Long employeeId) {
        employeeBizService.delete(employeeId);
        return CommonResult.success();
    }

    @Operation(summary = "删除员工", description = "删除员工")
    @GetMapping("employees/{employeeId}/decrypt-phone")
    public CommonResult<String> decryptPhone(@Parameter(description = "用户ID") @PathVariable("employeeId") Long employeeId) {
        EmployeeDTO employeeDTO = employeeBizService.getById(employeeId);
        AssertUtils.notNull(employeeDTO, "员工不存在");
        return CommonResult.success(AesEncryptUtils.decrypt(employeeDTO.getPhoneCrypto()));
    }

    @Operation(summary = "员工列表", description = "员工列表")
    @GetMapping("employees")
    public CommonResult<PageResponse<EmployeeResp>> page(
            @Validated PageReq pagReq,
            @RequestParam(value = "account", required = false) String account,
            @RequestParam(value = "name", required = false) String name) {
        EmployeeCondition condition = new EmployeeCondition();
        condition.setName(name);
        condition.setAccount(account);
        condition.setPageNum(pagReq.getPageNum());
        condition.setPageSize(pagReq.getPageSize());
        IPage<EmployeeDTO> page = employeeBizService.employeePage(condition);
        return CommonResult.success(PageResponse.convert(page, employeeConverter::dto2EmployeeResp));
    }


}
